package MindMap;

import javax.microedition.lcdui.*;

// VIEW

/** @author Lewis Marhin */
public class userInterface extends Canvas
{
    final int WHITE = 0xFFFFFF;
    final int BLACK = 0x000000;
    final int ANCHOR = 0;
    final int HEIGHT = 20; /** Height of each node for one line of text. */
    final int RANGLE = 5; /** Degree to which the rectangle is rounded. */
    final int REP = 2; /** Width of border of root node. */
    
    mindmapModel model = new mindmapModel("myRoot");

    protected void paint(Graphics g)
    {
        // Create background canvas
        g.setColor(WHITE);
        g.fillRect(0, 0, 300, 300);
        
        // Get root and nodes
        node root = model.getRoot();
        node[] list = model.getNodeList();
        
        // Draw root
        g.setColor(BLACK);
        for(int i = 0; i < REP; i++)
        {
        g.drawRoundRect(root.getXloc(), root.getYloc(),
                    width(root) + i, HEIGHT + i, 5, 5);
        }
        g.drawString(root.getDesc(), root.getXloc() + 3,
                    root.getYloc() + 3, ANCHOR);
        
        //Draw connectors
        for(int i = 1; i < model.getNodeCount(); i++)
        {
            node parent = list[i].getParent();
            g.drawLine(list[i].getXloc(), list[i].getYloc(), parent.getXloc(), parent.getYloc());
        }
        
        // Draw nodes (to prevent connectors from showing through, need to draw
        //              a filled rectangle of same width and height)
        for(int i = 1; i < model.getNodeCount(); i++)
        {
            g.setColor(WHITE);
            g.fillRoundRect(list[i].getXloc(), list[i].getYloc(),
                    width(list[i]), HEIGHT, RANGLE, RANGLE);
            
            g.setColor(BLACK);
            g.drawRoundRect(list[i].getXloc(), list[i].getYloc(),
                    width(list[i]), HEIGHT, RANGLE, RANGLE);
            
            g.drawString(list[i].getDesc(), list[i].getXloc() + 3,
                    list[i].getYloc() + 3, ANCHOR);
        }
    }
    
    private int width(node n)
    {
        return (n.getDesc().length() + 5) * 4;
    }
}
